package sampleDataManage

import (
	"bufio"
	"fmt"
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
	"github.com/flipped-aurora/gin-vue-admin/server/model/sampleDataManage"
	sampleDataManageReq "github.com/flipped-aurora/gin-vue-admin/server/model/sampleDataManage/request"
	"github.com/flipped-aurora/gin-vue-admin/server/service"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"os"
	"strings"
)

type SampleDataApi struct {
}

var sampleDataService = service.ServiceGroupApp.SampleDataManageServiceGroup.SampleDataService

// CreateSampleData 创建SampleData
// @Tags SampleData
// @Summary 创建SampleData
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body sampleDataManage.SampleData true "创建SampleData"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /sampleData/createSampleData [post]
func (sampleDataApi *SampleDataApi) CreateSampleData(c *gin.Context) {
	var sampleData sampleDataManage.SampleData
	err := c.ShouldBindJSON(&sampleData)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := sampleDataService.CreateSampleData(&sampleData); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// DeleteSampleData 删除SampleData
// @Tags SampleData
// @Summary 删除SampleData
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body sampleDataManage.SampleData true "删除SampleData"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /sampleData/deleteSampleData [delete]
func (sampleDataApi *SampleDataApi) DeleteSampleData(c *gin.Context) {
	var sampleData sampleDataManage.SampleData
	err := c.ShouldBindJSON(&sampleData)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := sampleDataService.DeleteSampleData(sampleData); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// DeleteSampleDataByIds 批量删除SampleData
// @Tags SampleData
// @Summary 批量删除SampleData
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除SampleData"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /sampleData/deleteSampleDataByIds [delete]
func (sampleDataApi *SampleDataApi) DeleteSampleDataByIds(c *gin.Context) {
	var IDS request.IdsReq
	err := c.ShouldBindJSON(&IDS)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := sampleDataService.DeleteSampleDataByIds(IDS); err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdateSampleData 更新SampleData
// @Tags SampleData
// @Summary 更新SampleData
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body sampleDataManage.SampleData true "更新SampleData"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /sampleData/updateSampleData [put]
func (sampleDataApi *SampleDataApi) UpdateSampleData(c *gin.Context) {
	var sampleData sampleDataManage.SampleData
	err := c.ShouldBindJSON(&sampleData)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := sampleDataService.UpdateSampleData(sampleData); err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindSampleData 用id查询SampleData
// @Tags SampleData
// @Summary 用id查询SampleData
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query sampleDataManage.SampleData true "用id查询SampleData"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /sampleData/findSampleData [get]
func (sampleDataApi *SampleDataApi) FindSampleData(c *gin.Context) {
	var sampleData sampleDataManage.SampleData
	err := c.ShouldBindQuery(&sampleData)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if resampleData, err := sampleDataService.GetSampleData(sampleData.ID); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"resampleData": resampleData}, c)
	}
}

// GetSampleDataList 分页获取SampleData列表
// @Tags SampleData
// @Summary 分页获取SampleData列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query sampleDataManageReq.SampleDataSearch true "分页获取SampleData列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /sampleData/getSampleDataList [get]
func (sampleDataApi *SampleDataApi) GetSampleDataList(c *gin.Context) {
	var pageInfo sampleDataManageReq.SampleDataSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := sampleDataService.GetSampleDataInfoList(pageInfo); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}

// @summary 统计国家出现次数
func (sampleDataApi *SampleDataApi) GetPeopleNameCount(c *gin.Context) {
	if list, err := sampleDataService.GetPeopleNameCountList(); err != nil {
		global.GVA_LOG.Error("获取失败", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithData(gin.H{"peopleNameCount": list}, c)
	}
}
func (sampleDataApi *SampleDataApi) FindByIds(c *gin.Context) {
	var IDS request.IdsReq
	err := c.ShouldBindJSON(&IDS)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := sampleDataService.FindByIds(IDS); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:  list,
			Total: total,
		}, "获取成功", c)
	}
}

// GetFileLineCount txt预览
func (sampleDataApi *SampleDataApi) GetFileLineCount(c *gin.Context) {
	fmt.Println("进入查询行数")
	var sampleData sampleDataManage.SampleData
	err := c.ShouldBindJSON(&sampleData)
	fmt.Println("解析的文件路径=" + sampleData.Symc)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	//file, _ := os.Open("E:\\Coding/Golang/sampledata/sample-data/server/uploads/file/" + sampleData.Symc)
	/**
	上传的文件存储在项目名/server/uploads/file目录下，linux获取文件完整路径名的命令是readlink -f 文件名
	*/
	//本地电脑上使用下面这个绝对路径
	//var filePathPre = "D:\\Coding/sample-data/server/uploads/file/"
	//var filePathPre = "E:\\Coding/sampleData/sample-data/server/uploads/file/"
	var filePathPre = "D:\\Project\\test\\sample_data\\server\\uploads\\file\\"
	//var filePathPre = "E:\\2024demo\\sample_data\\server\\uploads\\file"
	//部署到服务器上使用下面这个绝对路径
	//var filePathPre = "/root/sampleData/sampledata/server/uploads/file/"
	//var filePathPre = "/home/sampledata20241008/server/uploads/file/"
	file, err := os.OpenFile(filePathPre+sampleData.Symc, os.O_RDONLY, 0666)
	if err != nil {
		fmt.Println("读取文件失败")
		return
	}
	fileScanner := bufio.NewScanner(file)
	lineCount := 0
	for fileScanner.Scan() {
		lineTxt := strings.TrimSpace(fileScanner.Text())
		if len(lineTxt) == 0 {
			continue
		}
		strs := strings.Split(lineTxt, " ")
		lineCount += len(strs)
	}
	//fmt.Println("number of lines:", lineCount)
	//return lineCount

	//if list, total, err := sampleDataService.FindByIds(IDS); err != nil {
	//	global.GVA_LOG.Error("获取失败!", zap.Error(err))
	//	response.FailWithMessage("获取失败", c)
	//} else {
	fmt.Println("文件数据行数=%d", lineCount)
	response.OkWithDetailed(response.PageResult{
		Total: int64(lineCount),
		//List:  list,
		//Total: total,
	}, "获取成功", c)
	//}
}
